/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.util;

import br.com.beans.EnderecoCadastro;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Daniel
 */
public class ConsultarEndereco {

    public EnderecoCadastro ColsultaEndereco(String cep) 
    {        
        EnderecoCadastro endereco = new EnderecoCadastro();
        
        if(!(cep == null))
        {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/endereco", "root", "123456");

            PreparedStatement pstm = con.prepareStatement("select * from enderecoconsulta where cep = ?");
            pstm.setString(1, cep.replace(".", "").replace("-", ""));
            ResultSet rs = pstm.executeQuery();
            System.out.println("Pesquisando endereço");
            if (rs.next()) {
                endereco.setBairro(rs.getString("Bairro"));
                endereco.setCep(rs.getString("Cep"));
                endereco.setCidade(rs.getString("Cidade"));
                endereco.setUf(rs.getString("uf"));
                endereco.setRua(rs.getString("Logradouro"));
            }
            rs.close();
            pstm.close();
            con.close();
            return endereco;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ConsultarEndereco.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (SQLException ex) {
            Logger.getLogger(ConsultarEndereco.class.getName()).log(Level.SEVERE, null, ex);
        }    
        return null;
        }
    return null; 
    }
    
}
